package gateway.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import gateway.atguigu.gmall.model.product.*;
import gateway.atguigu.gmall.product.service.MangeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gateway.atguigu.gmall.product.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BaseMangeServiceImpl implements MangeService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BasettriInfoMapper basettriInfoMapper;


    @Override
    public List<BaseCategory1> getCategory1() {

        List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
       return baseCategory1s;
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {

            QueryWrapper  queryWrapper=     new QueryWrapper<BaseCategory2>();
        queryWrapper.eq("category1_id",category1Id);

     List<BaseCategory2>  baseCategory2List= baseCategory2Mapper.selectList(queryWrapper);
        return baseCategory2List;
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        QueryWrapper queryWrapper2 = new QueryWrapper<BaseCategory3>();
        queryWrapper2.eq("category2_id",category2Id);
      List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(queryWrapper2);
        return baseCategory3List;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo>  baseAttrInfoList = basettriInfoMapper.selectBaseAttrInfoList(category1Id,
               category2Id,category3Id);
        return baseAttrInfoList;
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
//        1判断是删除还是添加
        if (baseAttrInfo.getId() !=null) {
            basettriInfoMapper.updateById(baseAttrInfo);
        } else {
            basettriInfoMapper.insert(baseAttrInfo);
        }

        //删除原来的数据

        QueryWrapper mapper = new QueryWrapper<BaseAttrValue>();
        mapper.eq("attr_id",baseAttrInfo.getId());
        baseAttrValueMapper.delete(mapper);

        //添加
        //获取传递过来的属性值
      List<BaseAttrValue>  baseAttrValueList = baseAttrInfo.getAttrValueList();
            if (baseAttrValueList !=null && baseAttrValueList.size() > 0){
                for (BaseAttrValue baseAttrValue : baseAttrValueList) {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
              baseAttrValueMapper.insert(baseAttrValue);
                }
            }

    }


}
